package com.jtpay.ysfproductionprocess.frida;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.jtpay.ysfproductionprocess.R;
import com.jtpay.ysfproductionprocess.utils.AppApplication;
import com.jtpay.ysfproductionprocess.utils.ShellUtil;

import static com.jtpay.ysfproductionprocess.utils.PayContext.*;

public class FridaHelper {
    private static final String COMMAND_START_FRIDA_SERVER = "./data/local/tmp/frida-server &";

    public static void startFridaServer() {

        final boolean hasRoot = ShellUtil.hasRootPermission();
        sendLogMsg("startFridaServer hasRoot11 = " + hasRoot);
        if (!hasRoot) {
            sendToastMsg(AppApplication.getContext().getResources().getString(R.string.no_root_tip));
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                ShellUtil.CommandResult result = ShellUtil.execCommand(
                        COMMAND_START_FRIDA_SERVER, true, true);
                sendLogMsg("startFridaServer result = " + result.toString());
            }
        }).start();
    }

    /**
     * @param content send log content
     */
    private static void sendLogMsg(String content) {
        Intent intent = new Intent(ACTION_UNION_PAY_SHOW_LOG);
        intent.putExtra(BROADCAST_CONTENT_KEY, content);
        LocalBroadcastManager.getInstance(AppApplication.getContext()).sendBroadcast(intent);
    }

    private static void sendToastMsg(String content) {
        Intent intent = new Intent(ACTION_UNION_PAY_SHOW_TOAST);
        intent.putExtra(BROADCAST_CONTENT_KEY, content);
        LocalBroadcastManager.getInstance(AppApplication.getContext()).sendBroadcast(intent);
    }
}
